package demo.java.java8.Lambdas.demo5;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

public class Transaction {
	private final Trader trader;
	private final int year;
	private final int value;
	
	public Transaction(Trader t,int y,int v) {
		this.trader=t;
		this.year=y;
		this.value=v;
	}

	public String toString(){
		return "{" + this.trader + ", "+
				"year:"+this.year+", "+
				"value:"+this.value+"}";
	}

	public Trader getTrader() {
		return trader;
	}

	public int getYear() {
		return year;
	}

	public int getValue() {
		return value;
	}
	
	public static void main(String[] args) {
		Trader raoul=new Trader("Raoul", "Cambridge");
		Trader mario=new Trader("Mario", "Milan");
		Trader alan=new Trader("Alan", "Cambridge");
		Trader brian=new Trader("Brian", "Cambridge");
		
		List<Transaction> transactions=Arrays.asList(
				new Transaction(brian, 2011, 300),
				new Transaction(raoul, 2012, 1000),
				new Transaction(raoul, 2011, 400),
				new Transaction(mario, 2012, 710),
				new Transaction(mario, 2012, 700),
				new Transaction(alan, 2012, 950)
				);
		
//		(1) 找出2011年发生的所有交易，并按交易额排序（从低到高）。
		List<Transaction> demo1=transactions.stream()
				.filter(t -> t.getYear()==2011)
				.sorted(Comparator.comparing(Transaction :: getValue))
				.distinct()
				.collect(Collectors.toList());
		System.out.println("======demo1=====");
		System.out.println(demo1);
		
//		(2) 交易员都在哪些不同的城市工作过？
		List<String> list=transactions.stream()
				.map(t -> t.getTrader().getCity()).distinct()
				.collect(Collectors.toList());
		System.out.println("======demo2=====");
		System.out.println(list);
		
//		(3) 查找所有来自于剑桥的交易员，并按姓名排序。
		List<Trader> demo3=transactions.stream()
				.map(Transaction :: getTrader)
				.filter(t -> t.getCity().equals("Cambridge"))
				.sorted(Comparator.comparing(Trader::getName))
				.collect(Collectors.toList());
		System.out.println("======demo3=====");
		demo3.forEach((t) -> System.out.println(t.getName()));
		System.out.println(demo3);
		
//		(4) 返回所有交易员的姓名字符串，按字母顺序排序。
		List<String> demo4=transactions.stream()
				.map(t -> t.getTrader().getName())
				.sorted()
				.distinct()
				.collect(Collectors.toList());
		System.out.println("======demo4=====");
		System.out.println(demo4);
		
//		(5) 有没有交易员是在米兰工作的？
		boolean demo5=transactions.stream()
				.anyMatch(t -> t.getTrader().getCity().equals("Milan"));
		System.out.println("======demo5=====");
		System.out.println(demo5);
		
//		(6) 打印生活在剑桥的交易员的所有交易额。
		System.out.println("======demo6=====");
		transactions.stream()
		.filter(t -> t.getTrader().getCity().equals("Cambridge"))
		.map(Transaction :: getValue)
		.forEach(System.out::println);
		
//		(7) 所有交易中，最高的交易额是多少？
		int demo7=transactions.stream()
				.map(Transaction::getValue)
				.reduce(0,Integer::max);
		System.out.println("======demo7=====");
		System.out.println(demo7);
		
//		(8) 找到交易额最小的交易。
		Optional<Transaction> demo8=transactions.stream()
				.reduce((t1,t2) -> t1.getValue() < t2.getValue() ? t1 : t2);
		System.out.println("======demo8=====");
		System.out.println(demo8);
	}
}
